package com.example.coreshopapp.utils;

import java.lang.reflect.Field;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class FormDataUtils {
    public static RequestBody buildFormData(Object entity) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);

        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true); // 允许访问 private 字段
            try {
                Object value = field.get(entity);
                if (value != null) {
                    builder.addFormDataPart(field.getName(), value.toString());
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return builder.build();
    }

}
